//
//  NNSegmentedViewTool.swift
//  DYLive
//
//  Created by 众力金融 on 2019/5/8.
//  Copyright © 2019 众力金融. All rights reserved.
//

import UIKit

public extension UIColor {
    var nn_red: CGFloat {
        var r: CGFloat = 0
        getRed(&r, green: nil, blue: nil, alpha: nil)
        return r
    }
    
    var nn_green: CGFloat {
        var g: CGFloat = 0
        getRed(nil, green: &g, blue: nil, alpha: nil)
        return g
    }
    
    var nn_blue: CGFloat {
        var b: CGFloat = 0
        getRed(nil, green: nil, blue: &b, alpha: nil)
        return b
    }
    
    var nn_alpha: CGFloat {
        return cgColor.alpha
    }
}


public class NNSegmentedViewTool {
    
    public static func interpolate<T: SignedNumeric & Comparable>(from: T, to: T, percent: T) -> T {
        let percent = max(0, min(1, percent))
        return from + (to - from) * percent
        
    }
    
    public static func interpolateColor(from: UIColor, to: UIColor, precent: CGFloat) -> UIColor {
        let r = interpolate(from: from.nn_red, to: to.nn_red, percent: precent)
        let g = interpolate(from: from.nn_green, to: to.nn_green, percent: CGFloat(precent))
        let b = interpolate(from: from.nn_blue, to: to.nn_blue, percent: CGFloat(precent))
        let a = interpolate(from: from.nn_alpha, to: to.nn_alpha, percent: CGFloat(precent))
        return UIColor(red: r, green: g, blue: b, alpha: a)
    }
    
}
